/*
 *  src/chapter1/server.c
 *
 *  Harry Wei <harryxiyou@gmail.com> 2011 (C)
 */
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>

#define MAXLINE		(50)

int main(int argc, char **argv)
{
	int listenfd;
	int connfd;
	time_t ticks;
	char buff[MAXLINE];
	struct sockaddr_in servaddr;

	listenfd = socket(AF_INET, SOCK_STREAM, 0);
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(13);
	bind(listenfd, (struct sockaddr_in *) &servaddr, sizeof(servaddr));
	listen(listenfd, LISTENQ);
	while (1) {
		connfd = accept(listenfd, (struct servaddr_in *) NULL, NULL);
		ticks = time(NULL);
		snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
		write(connfdm, buff, strlen(buff));
		close(connfd);
	}
}
